//
//  NYDetailContentDesCell.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/10.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYDetailContentDesCell.h"
#import "NYCourseModel.h"
#import "UIImage+UrlSize.h"
@interface NYDetailContentDesCell ()
@property(nonatomic, strong) DTAttributedTextContentView *titleAttrView;
@property(nonatomic, strong) NYHtmlHelper *htmlHelper;
@end
@implementation NYDetailContentDesCell
//+ (CGFloat)heightForObject:(NICellObject *)object identifier:(NSString *)identifier atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
//{
//
//        return [tableView fd_heightForCellWithIdentifier:identifier cacheByKey:[NSString stringWithFormat:@"%@_%@",indexPath,[(NYCourseModel *)object.userInfo modelDescription].md5String] configuration:^(NYDetailContentDesCell * cell) {
//                [cell updateContentWithModel:object.userInfo];
//        }];
//}
+ (BOOL)shouldAppendIndexPathToReuseIdentifier
{
        return YES;
}
- (BOOL)shouldUpdateCellWithObject:(NICellObject *)object atIndexPath:(NSIndexPath *)indexPath
{
        [self updateContentWithModel:object.userInfo];
        return YES;
}

- (void)updateContentWithModel:(NYCourseModel *)model
{
        [super updateContent];
        
        NSString *resString = model.video_describe.copy;
//
//        NSRange range = [resString rangeOfString:@"<body>"];
//        NSString *replaceString = [NSString stringWithFormat:@"<body>%d.",(int)self.indexPath.section + 1];
//        if (range.location == NSNotFound) {
//                range = [resString rangeOfString:@"<p>"];
//                replaceString = [NSString stringWithFormat:@"<p>%d.",(int)self.indexPath.section + 1];
//        }
//        if (range.location != 0) {
//                resString = [NSString stringWithFormat:@"%d.%@",(int)self.indexPath.section + 1,resString];
//        } else {
//                resString = [resString stringByReplacingCharactersInRange:range withString:replaceString];
//        }
//
        resString = [self.htmlHelper getImgSizeAndReplaceHtmlString:resString];
        
        self.titleAttrView.attributedString = [self.htmlHelper attributedStringForHtmlString:resString maxImageSize:CGSizeMake(__WGWidth(self.width), CGFLOAT_MAX)];
        CGFloat suggestHeight = [self.titleAttrView  suggestedFrameSizeToFitEntireStringConstraintedToWidth:__MAIN_SCREEN_WIDTH__ - __WGWidth(50)].height;
        [self.titleAttrView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(self.contentView).valueOffset([NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(__WGHeight(16), __WGWidth(16), __WGHeight(16), __WGWidth(16))]);
                make.height.mas_equalTo(suggestHeight);
        }];
        
}
- (void)setupView
{
        [super setupView];
        [self.contentView addSubview:self.titleAttrView];
        [self.titleAttrView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(self.contentView).valueOffset([NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(__WGHeight(16), __WGWidth(16), __WGHeight(16), __WGWidth(16))]);
        }];
}

/// MARK: - Lazy
- (DTAttributedTextContentView *)titleAttrView
{
        if (_titleAttrView == nil) {
                _titleAttrView = [self.htmlHelper createAttributedContentView];
        }
        return _titleAttrView;
}

- (NYHtmlHelper *)htmlHelper
{
        if (_htmlHelper == nil) {
                _htmlHelper = [[NYHtmlHelper alloc] init];
        }
        return _htmlHelper;
}
@end
